package com.yinian.cart.service.OrderList;

import com.yinian.cart.entity.OrderList;
import com.yinian.cart.entity.ProductList;
import com.yinian.cart.repository.OrderListRepository;
import com.yinian.cart.repository.ProductListRepository;
import jakarta.transaction.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.UUID;

@Service
@Transactional
public class OrderListServiceImpl implements OrderListService{
    @Autowired
    private OrderListRepository orderListRepository;
    @Autowired
    private ProductListRepository productListRepository;
    @Override
    public OrderList addNewOrderList(String productId, int productCount, int userAccount) {
//        获取一下商品的价格
        ProductList productList = productListRepository.findByProductId(productId);
        OrderList orderList = new OrderList();
        orderList.setDataId(UUID.randomUUID().toString());
        orderList.setUserAccount(userAccount);
        orderList.setProductCount(productCount);
        orderList.setProductId(productId);
        orderList.setOrderPrice(productList.getProductPrice());
        orderList.setOrderId(UUID.randomUUID().toString());
        orderList.setOrderTime(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        OrderList orderList1 = orderListRepository.save(orderList);
        return orderList1;
    }

    @Override
    public List<OrderList> getAllOrderList(int userAccount) {
        List<OrderList> list = orderListRepository.findAllByUserAccountOrderByOrderTimeDesc(userAccount);
        return list;
    }


}
